﻿using System.Configuration;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using VietThinks.Mapping.Maps;
using NHibernate.Dialect;
using Configuration = NHibernate.Cfg.Configuration;

namespace VietThinks.Mapping
{
    public class RepositoryFactoryBuilder_MsSql : RepositoryFactoryBuilderBase
    {
        /// <summary>
        /// Configures throw-away storage for automation testing
        /// </summary>
        /// <returns></returns>
        protected override Configuration ConfigureCore()
        {
            this.dialect = new MsSql2005Dialect();

            var databaseConfiguration = MsSqlConfiguration.MsSql2005
                .ConnectionString(cs => cs.Server(ConfigurationManager.AppSettings["DatabaseServerName"])
                                            .Database(ConfigurationManager.AppSettings["DatabaseName"])
                                            .Username(ConfigurationManager.AppSettings["DatabaseUsername"])
                                            .Password(ConfigurationManager.AppSettings["DatabasePassword"]))
                                            .AdoNetBatchSize(10)
                .Cache(c => c.UseQueryCache())
                .UseReflectionOptimizer();

            return Fluently.Configure()
                .Database(databaseConfiguration)
                .Mappings(cfg => cfg.FluentMappings.AddFromAssemblyOf<RoleMap>())
                .BuildConfiguration();
        }
    }
}
